<?php
namespace Admin\Controller;

// use Vendor\Tree;

class ScoreController extends ComController
{
    public function index()
    {
        //积分兑换明细
        $p   = I('get.p', 1, 'intval');
        $keyword = I('get.keyword','');
        $type = I('get.type',0, 'trim');

        $m = M('score_tran');
        $map = array(
            'businessid' => '10202'
        );

        if (!empty($keyword)) {
            switch ($type) {
                case 'phone':
                    break;
                    case 'user':
                    $map['uname'] = array('like',"%$keyword%");
                default:
                    break;
            }
        }

        $psize = 20;

        $list = $m->where($map)->order('createtime DESC')->page($p, $psize)->select();
        $count = $m->where($map)->count();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();

        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->display();
    }

    public function tranlog()
    {
        $p         = I('get.p', 1, 'intval');
        $keyword   = trim(I('keyword', ''));
        $m         = M('score_tran');
        $starttime = I('get.starttime', '');
        $endtime   = I('get.endtime', '');

        $map   = array();

        if (!empty($keyword)) {
            $map['uname'] = array('like', "%$keyword%");
        }

        if (!empty($starttime)) {
            $map['createtime'][] = array('gt', $starttime);
        }

        if (!empty($endtime)) {
            $map['createtime'][] = array('lt', $endtime);
        }

        $psize = 20;

        $list  = $m->alias('s')
        ->field('s.*,m.user opername')
        ->join("LEFT JOIN __MEMBER__ m ON m.uid=s.operId")
        ->where($map)->order('s.createTime DESC')->page($p, $psize)->select();

        $count = $m->where($map)->count();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();

        $this->assign('list', $list);
        $this->assign('page', $page);

        return $this->display();
    }


    /**
     * 导出积分异动记录
     * @return [type] [description]
     */
    public function exporttran()
    {
        $username   = I('param.keyword','');
        $starttime  = I('param.starttime','');
        $endtime    = I('param.endtime','');

        ini_set('max_execution_time', '200');
        // set_time_limit(120);
        Vendor('PHPExcel.PHPExcel');
        Vendor('PHPExcel.PHPExcel.IOFactory');
        $objPHPExcel = new \PHPExcel();

        $m = M('score_tran');

        if (!empty($starttime)) {
            $map['createtime'][] = array('gt',$starttime);
        }

        if (!empty($endtime)) {
            $map['createtime'][] = array('lt',$endtime);
        }

        if (!empty($username)) {
            $map['uname'] = $username;
        }

        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'ID')
        ->setCellValue('B1', '会员名')
        ->setCellValue('C1', '积分')
        ->setCellValue('D1', '异动类型')
        ->setCellValue('E1', '积分余额')
        ->setCellValue('F1', '操作人')
        ->setCellValue('G1', '说明')
        ->setCellValue('H1', '异动时间');

        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(30);

        $fileName = date('YmdHis').mt_rand(1000,9999);
        $fileName = iconv("utf-8", "gb2312", $fileName);

        $list  = $m->alias('s')
        ->field('s.*,m.user opername')
        ->join("LEFT JOIN __MEMBER__ m ON m.uid=s.operId")
        ->where($map)->order('s.createTime DESC')->select();

        if (!empty($list)) {
            foreach ($list as $k=>$v) {
                $newKey = $k+2;
                $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$newKey, $v['id'])
                ->setCellValue('B'.$newKey, $v['uname'])
                ->setCellValue('C'.$newKey, $v['score'])
                ->setCellValue('D'.$newKey, $v['type'] > 1 ? '增' : '减')
                ->setCellValue('E'.$newKey, $v['balance'])
                ->setCellValue('F'.$newKey, $v['opername'])
                ->setCellValue('G'.$newKey, $v['memo'])
                ->setCellValue('H'.$newKey, $v['createtime']);
            }
        }

        $title = '积分异动明细';
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle($title);

        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $title . '.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        ob_end_clean();
        addlog('导出积分异动记录');
        $objWriter->save('php://output');
        exit;

    }

    /**
     * 导出兑换明细
     * @return [type] [description]
     */
    public function exportech()
    {
        $conditions = I('param.');
        $username   = I('param.username','');
        $starttime  = I('param.starttime','');
        $endtime    = I('param.endtime','');

        ini_set('max_execution_time', '200');
        // set_time_limit(120);
        Vendor('PHPExcel.PHPExcel');
        Vendor('PHPExcel.PHPExcel.IOFactory');
        $objPHPExcel = new \PHPExcel();

        $map = array(
            'businessid' => 10202
        );

        $m = M('score_tran');
        if (!empty($starttime)) {
            $map['createtime'][] = array('gt',$starttime);
        }

        if (!empty($endtime)) {
            $map['createtime'][] = array('lt',$endtime);
        }

        if (!empty($username)) {
            $map['uname'] = $username;
        }

        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'ID')
        ->setCellValue('B1', '会员名')
        ->setCellValue('C1', '兑换使用积分')
        ->setCellValue('D1', '兑换时间');

        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);

        $fileName = date('YmdHis').mt_rand(1000,9999);
        $fileName = iconv("utf-8", "gb2312", $fileName);

        $list = $m->where($map)->order('id desc')->select();

        if (!empty($list)) {
            foreach ($list as $k=>$v) {
                $newKey = $k+2;
                $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$newKey, $v['id'])
                ->setCellValue('B'.$newKey, $v['uname'])
                ->setCellValue('C'.$newKey, $v['score'])
                ->setCellValue('D'.$newKey, $v['createtime']);
            }
        }

        $title = '积分兑换明细';
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle($title);

        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $title . '.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        ob_end_clean();
        addlog('导出积分兑换明细');
        $objWriter->save('php://output');
        exit;
    }
}
